package com.nes.springcloud.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class HelloService {

    @Autowired
    private RestTemplate restTemplate;

    public String getResult(Integer a,Integer b) {
        //通过服务名访问
        return restTemplate.getForObject("http://eureka-client-1/eureka/add?a="+a+"&b="+b,String.class);
    }

    //如果restTemplate断开或者超时，就调用回调函数getResultError
    @HystrixCommand(fallbackMethod = "getResultError")
    public String getResultBreak(Integer a,Integer b) {
        //通过服务名访问
        return restTemplate.getForObject("http://eureka-client-1/eureka/add?a="+a+"&b="+b,String.class);
    }

    public String getResultError(Integer a,Integer b) {
        return "计算 ("+a+"+"+b+") error...";
    }
}